<template>
  <div>
    <el-button type="primary" @click="showDialog('add')">新增一条</el-button>
    <el-table
      :data="list"
      class="ymd-table"
    >
      <el-table-column
        prop="name"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="url"
        label="链接地址"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
      >
        <template scope="scope">
          <el-button
            size="small"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="currentPageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="滚动广告" :visible.sync="dialogVisible">
      <el-form
        :model="form"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="名称">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="链接地址">
          <el-input v-model="form.url" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hideDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as apis from 'services/advertisement'

export default {
  name: 'ScrollAd',
  data() {
    return {
      form: {
        name: '',
        url: '',
      },
      dialogVisible: false,
      list: [],
      currentPage: 1,
      currentPageSize: 20,
      total: 0,
    }
  },
  created() {
    this.getAdList()
  },
  methods: {
    async getAdList(params = {}) {
      const res = await apis.getAdList(params)
      this.list = res.data
      this.total = res.total
    },
    showDialog(type) {
      if (type === 'add') {
        this.form = {
          name: '',
          url: '',
        }
        this.id = undefined
      }
      this.dialogVisible = true
    },
    hideDialog() {
      this.dialogVisible = false
    },
    handleEdit(row) {
      this.form.name = row.name
      this.form.url = row.url
      this.id = row.id
      this.showDialog()
    },
    async handleDelete(row) {
      const isDel = await this.$confirm(`此操作将删除该${row.name}数据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      if (isDel) {
        await apis.deleteAdById(row.id)
        this.getAdList({
          pageSize: this.pageSize,
          page: this.currentPage,
        })
      }
    },
    handleSizeChange(pageSize) {
      this.currentPageSize = pageSize
      this.getAdList({
        pageSize,
        page: this.currentPage,
      })
    },
    handleCurrentChange(page) {
      this.currentPage = page
      this.getAdList({
        pageSize: this.currentPageSize,
        page,
      })
    },
    confirm() {
      const payload = {
        ...this.form,
      }
      if (this.id) {
        payload.id = this.id
      }

      apis.saveAd(payload)
        .then(() => {
          this.$message({
            type: 'success',
            message: '保存成功!',
          })
          this.hideDialog()
          this.getAdList({
            pageSize: this.pageSize,
            page: this.currentPage,
          })
        })
        .catch(() => {
          this.$message({
            type: 'error',
            message: '保存失败',
          })
        })
    },
  },
}
</script>

<style>

</style>
